/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.jdbc.tuple.rs.column;

import io.trino.jdbc.tuple.rs.DataType;

import java.util.AbstractMap;
import java.util.Map;

/**
 * @author fengyang(yueny09 @ 163.com)
 * @date 2022年06月08日 上午11:18
 */
public class ColumnDataParser {
    /**
     * 将数据值转换为 columnType对应类型和类型值
     *
     * @param source  数据值
     * @param columnType  转换的mysql 字段类型， 为 java.sql.Types
     * @return
     */
    public static final Map.Entry<Object, DataType> parserDataType(Object source, int columnType){
        DataType dataType = DataType.getBySqlType(columnType);
        if(dataType == null){
            // 不支持的类型， 返回 null 做特殊标识处理
            return null;
        }

        Object value = dataType.getSourceActualyValue(source);

        return new AbstractMap.SimpleEntry<>(value, dataType);
    }

}
